package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunBodySensorConfigMapper;
import com.csun.cmny.provider.device.csun.gateway.GatewayMessageSender;
import com.csun.cmny.provider.model.constant.DeviceConstant;
import com.csun.cmny.provider.model.constant.LifeConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.bodysensor.BodySensorConfigDto;
import com.csun.cmny.provider.model.vo.csunbody.BodySensorConfigVO;
import com.csun.cmny.provider.model.vo.life.LifeTransducerConfigVO;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class DmcCsunBodySensorConfigServiceImpl extends BaseService<DmcCsunBodySensorConfig> implements DmcCsunBodySensorConfigService {

    @Resource
    private DmcCsunBodySensorConfigMapper dmcCsunBodySensorConfigMapper;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private DmcCsunBodySensorLocationService dmcCsunBodySensorLocationService;
    @Resource
    private DmcLifeTransducerRoomConfigService dmcLifeTransducerRoomConfigService;
    @Resource
    private DmcLifeTransducerDefaultConfigService dmcLifeTransducerDefaultConfigService;
    @Resource
    private GatewayMessageSender gatewayMessageSender;

    @Override
    public DmcCsunBodySensorConfig queryConfigByDeviceId(Long deviceId) {

        DmcCsunBodySensorConfig result = null;

        DmcDevice dmcDevice = dmcDeviceService.selectByKey(deviceId);
        if (PublicUtil.isEmpty(dmcDevice)) {
            return null;
        }

        // 从配置中获取
//        result = dmcCsunBodySensorConfigMapper.selectByPrimaryKey(deviceId);
//        if (PublicUtil.isNotEmpty(result)) {
//            return result;
//        }

        // 根据绑定方式获取配置
        DmcCsunBodySensorLocation dmcCsunBodySensorLocation = dmcCsunBodySensorLocationService
                .selectByKey(deviceId);
        if (PublicUtil.isEmpty(dmcCsunBodySensorLocation)) {
            return null;    // 设备尚未绑定
        }

        switch (dmcCsunBodySensorLocation.getBindType()) {
            case DeviceConstant.DEVICE_BIND_TYPE_LIFE :
                // 从房间配置中获取
                DmcLifeTransducerRoomConfig dmcLifeTransducerRoomConfig =
                        dmcLifeTransducerRoomConfigService.selectByKey(dmcCsunBodySensorLocation.getRoomId());
                if (PublicUtil.isEmpty(dmcLifeTransducerRoomConfig)) {
                    // 从该机构的配置中获取
                    DmcLifeTransducerDefaultConfig dmcLifeTransducerDefaultConfig =
                            dmcLifeTransducerDefaultConfigService.selectByKey(dmcDevice.getGroupId());
                    if (PublicUtil.isEmpty(dmcLifeTransducerDefaultConfig)) {
                        return null;
                    } else {
                        result = new DmcCsunBodySensorConfig();
                        result.setDeviceId(deviceId);
                        result.setToiletAttentionTime(dmcLifeTransducerDefaultConfig.getToiletAttentionTime());
                        result.setToiletWarningTime(dmcLifeTransducerDefaultConfig.getToiletWarningTime());
                        result.setToiletUnusualTime(dmcLifeTransducerDefaultConfig.getToiletUnusualTime());
                    }
                } else {
                    result = new DmcCsunBodySensorConfig();
                    result.setDeviceId(deviceId);
                    result.setToiletAttentionTime(dmcLifeTransducerRoomConfig.getToiletAttentionTime());
                    result.setToiletWarningTime(dmcLifeTransducerRoomConfig.getToiletWarningTime());
                    result.setToiletUnusualTime(dmcLifeTransducerRoomConfig.getToiletUnusualTime());
                }
                saveOrUpdate(result);
                break;
        }
        return result;
    }

    /**
     * 根据房间Id和机构Id获取温度传感器配置
     *
     *
     * @param deviceId
     * @param roomId
     * @param groupId
     * @return
     */
    @Override
    public DmcCsunBodySensorConfig queryConfigByRoomIdAndGroupId(Long deviceId, Integer roomId, Long groupId) {

        DmcLifeTransducerRoomConfig dmcLifeTransducerRoomConfig =
                dmcLifeTransducerRoomConfigService.selectByKey(roomId);

        DmcCsunBodySensorConfig dmcCsunBodySensorConfig = null;

        if (PublicUtil.isEmpty(dmcLifeTransducerRoomConfig)) {
            // 从该机构的配置中获取
            LifeTransducerConfigVO vo = dmcLifeTransducerDefaultConfigService.getDefaultConfigByGroupId(groupId);
//            if (PublicUtil.isEmpty(dmcLifeTransducerDefaultConfig)) {
//                return null;
//            } else {
                dmcCsunBodySensorConfig = new DmcCsunBodySensorConfig();
                dmcCsunBodySensorConfig.setDeviceId(deviceId);
                dmcCsunBodySensorConfig.setToiletUnusualTime(vo.getToiletUnusualTime());
                dmcCsunBodySensorConfig.setToiletWarningTime(vo.getToiletWarningTime());
                dmcCsunBodySensorConfig.setToiletAttentionTime(vo.getToiletAttentionTime());
                dmcCsunBodySensorConfig.setIsSend(LifeConstant.DEVICE_ORDER_NEED_SEND);
//            }
        } else {
            dmcCsunBodySensorConfig = new DmcCsunBodySensorConfig();
            dmcCsunBodySensorConfig.setDeviceId(deviceId);
            dmcCsunBodySensorConfig.setToiletUnusualTime(dmcLifeTransducerRoomConfig.getToiletUnusualTime());
            dmcCsunBodySensorConfig.setToiletWarningTime(dmcLifeTransducerRoomConfig.getToiletWarningTime());
            dmcCsunBodySensorConfig.setToiletAttentionTime(dmcLifeTransducerRoomConfig.getToiletAttentionTime());
        }
        //更新设备配置
        saveOrUpdate(dmcCsunBodySensorConfig);
        return dmcCsunBodySensorConfig;
    }

    @Override
    public Integer saveOrUpdate(DmcCsunBodySensorConfig dmcCsunBodySensorConfig) {
        DmcCsunBodySensorConfig config = selectByKey(dmcCsunBodySensorConfig.getDeviceId());
        if(config == null){
            return dmcCsunBodySensorConfigMapper.insertSelective(dmcCsunBodySensorConfig);
        }else{
            return dmcCsunBodySensorConfigMapper.updateByPrimaryKeySelective(dmcCsunBodySensorConfig);
        }
    }

    @Override
    public Integer setBodySensorConfig(BodySensorConfigDto dto, LoginAuthDto loginAuthDto) {

        List<Long> deviceIdList = dto.getDeviceIdList();
        int i = 0;

        for (Long deviceId : deviceIdList) {


                DmcCsunBodySensorConfig csunBodySensorConfig = selectByKey(deviceId);
                DmcCsunBodySensorConfig config = new DmcCsunBodySensorConfig();
                //放入数据
                BeanUtils.copyProperties(dto,config);
                config.setIsSend(LifeConstant.DEVICE_ORDER_NEED_SEND);
                config.setLastOperator(loginAuthDto.getUserName());
                config.setLastOperatorId(loginAuthDto.getUserId());
                config.setDeviceId(deviceId);

                gatewayMessageSender.setLifeBodySensorTimer(deviceId.toString(), dto.getToiletAttentionTime().toString(),dto.getToiletWarningTime().toString(),dto.getToiletUnusualTime().toString());

                //没有则新增、有则修改
                if(csunBodySensorConfig == null){
                    config.setCreator(loginAuthDto.getUserName());
                    config.setCreatorId(loginAuthDto.getUserId());
                    dmcCsunBodySensorConfigMapper.insertSelective(config);
                }else{
                    config.setUpdateTime(new Date());
                    dmcCsunBodySensorConfigMapper.updateByPrimaryKeySelective(config);
                }
                i++;



        }

        return i;
    }

    @Override
    public BodySensorConfigVO getBodySensorConfig(Long deviceId) {

        BodySensorConfigVO vo = new BodySensorConfigVO();
        DmcCsunBodySensorConfig dmcCsunBodySensorConfig = dmcCsunBodySensorConfigMapper.selectByPrimaryKey(deviceId);

        if(dmcCsunBodySensorConfig != null){
            BeanUtils.copyProperties(dmcCsunBodySensorConfig,vo);
        }
        return vo;
    }
}
